import random
user_database = {}
ku_num=100
def meaue():   #界面
    print('*'*30)
    print("*         中国工商银行         *")
    print("*         账户管理系统         *")
    print("*            v1.0            *")
    print('*' * 30)
    print("\n*1.开户                       *")
    print("*2.存钱                       *")
    print("*3.取钱                       *")
    print("*4.转账                       *")
    print("*5.查询                       *")
    print("*6.Bye！                      *")
    print('*'*30)
    while True:
        choice=int(input("请输入要选择的操作："))
        if choice==1:
            user_zhanghao=random.randint(10000000,99999999)
            user_name=input("请输入你的姓名：")
            password=int(input("请设置一个六位数密码:"))
            address=input("请输入你的家庭住址：")
            bank_name = '中国工商银行'
            money=5000
            num =add_user(user_zhanghao,user_name,password,address,bank_name,money)
            if num==1:
                print("1:添加成功")
                print(f"您的账号是：{user_zhanghao}，密码是：{password}")
            elif num==2:
                print("2:用户已存在！")
            elif num==3:
                print("3：用户库已满！")
        elif choice==2:
            user_zhanghao=int(input("账号："))
            saving_money = int(input("请输入存款金额："))
            if saving(user_zhanghao,saving_money):  #存款
                print("存款成功！")
                print(f"存款后该账户余额：{user_database[user_zhanghao]['money']}")
            else:
                print("账号输入错误！")
        elif choice==3:  #取款
            zhanghao=int(input("账号："))
            password=int(input("密码："))
            take_money=int(input("取钱金额："))
            take_num=take_fund(zhanghao,password,take_money)
            if take_num==1:
                print("账号不存在！")
            elif take_num==2:
                print("密码输入错误！")
            elif take_num==3:
                print("账户余额不足")
        elif choice==4:
            out_zhanghao=int(input("转出账号："))
            in_zhanghao=int(input("转入账号："))
            out_psw=int(input("转出账号密码："))
            out_money=int(input("转出金额"))
            transfer_num=transfer(out_zhanghao,in_zhanghao,out_psw,out_money)
            if transfer_num==1:
                print("转入或转出账号不存在！")
            elif transfer_num==2:
                print("密码不正确！")
            elif transfer_num==3:
                print("卡内余额不足！")
        elif choice==5:
            zhanghao=int(input("请输入要查询的账号："))
            password=int(input("请输入密码："))
            select_count(zhanghao,password)
        elif choice==6:
            print("Bye!欢迎下次光临！")
            break
        else:
            print("该操作不存在，请重新输入！")
            continue
#添加用户
def add_user(zh, name, pwd, addr, bank, money):
        """添加用户：1成功 / 2已存在 / 3库满"""
        if len(user_database) >= ku_num:
            return 3
        if zh in user_database:
            return 2
        user_database[zh] = {
            "name": name, "password": pwd, "address": addr,
            "bank": bank, "money": money
        }
        return 1
#存款
def saving(zhanghao,saving_money):
    if zhanghao in user_database:
        user_database[zhanghao]["money"]+=saving_money
        return True
    else:
        return False
#取款
def take_fund(zhanghao,psw,take_money):
    if zhanghao in user_database:
        if psw==user_database[zhanghao]["password"]:
            if user_database[zhanghao]["money"]>=take_money:
                user_database[zhanghao]["money"]-=take_money
                print(f"取款成功！账户还剩余额：{user_database[zhanghao]['money']}")
            else:
                return 3
        else:
            return 2
    else:
        return 1
#转账
def transfer(out_zhanghao,in_zhanghao,out_psw,out_money):
    if out_zhanghao in user_database:
        if in_zhanghao in user_database:
            if out_psw==user_database[out_zhanghao]["password"]:
                if out_money<=user_database[out_zhanghao]["money"]:
                    user_database[out_zhanghao]["money"]-=out_money
                    user_database[in_zhanghao]["money"]+=out_money
                    print("转账成功！")
                else:
                    return 3
            else:
                return 2
        else:
            return 1
    else:
        return 1
#查询
def select_count(zh,psw):
    if zh in user_database:
        if psw==user_database[zh]["password"]:
            print(f"当前账号为：{zh},密码为：{psw},余额：{user_database[zh]['money']}元,用户居住地址：{user_database[zh]['address']},当前账户的开户行：{user_database[zh]['bank']}")
        else:
            print("密码输入错误！")
    else:
        print("该用户不存在！")


meaue()  #显示菜单

